<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Team extends Controllers{
    public function index()
    {
        $user = Session::get("user");
    	$page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
    	$where = " where m.power = 2";
    	if($_REQUEST['uid']){
    	$where .= " and m.uid = '{$_REQUEST['uid']}'";
    	$this->assign("uid",$_REQUEST['uid']);
    	}
    	if($_REQUEST['nickname']){
		$where .= " and m.nickname like '%{$_REQUEST['nickname']}%'";
    	$this->assign("nickname",$_REQUEST['nickname']);
    	}

        if($user['power'] == 1){
            $where .= " and t.partner_id = {$user['uid']}";
        } else if ($user['power'] == 2){
            $where .= " and t.team_name = {$user['uid']}";

        }
        
         $count  = DB::query("select count(*) as count from ims_mc_members as m left join ims_xuan_zy_commission_team as t on m.uid = t.uid  {$where}")[0]['count'];
        $size = 20;
        $limit = ($page-1)*$size;
         $sql = "select m.nickname,m.avatar,m.uid ,  IFNULL(t.team_name,'空') as team_name, IFNULL(t.partner_name,'空') as partner_name from ims_mc_members as m  left join ims_xuan_zy_commission_team as t on m.uid = t.uid {$where} group by m.uid";
        $sql .= " limit $limit,$size";
         $arr = DB::query($sql);
         foreach ($arr as $k => &$v) {
           $sql = "select count(*) count from ims_xuan_zy_commission_team where team_id = {$v['uid']} ";
           $v['mem_count'] = DB::query($sql)[0]['count'];

        }
	    if(request()->isajax()){
    	 echo json_encode($arr);die;
        }

    
         $this->assign("count",$count);
    	 $this->assign("list",$arr);
    	 return $this->fetch("admin/team");
    }
    public function adduser(){
    $user = Session::get("user");
	$pindex    = max(1, intval($_REQUEST['page']));
	$psize     = 20;

	$wheres = " AND a.uniacid= 2 and a.power not in (1,2)";

	if(!empty($_REQUEST['id'])){

		$wheres.=" and a.uid= '{$_REQUEST['id']}'  ";
		$this->assign("id",$_REQUEST['id']);

		
	}
    if(!empty($_REQUEST['phone'])){

        $wheres.=" and a.mobile='{$_REQUEST['phone']}' ";
		$this->assign("phone",$_REQUEST['phone']);



    }
    if(!empty($_REQUEST['yewuy'])){

        $wheres.=" and b.yewuy= '{$_REQUEST['yewuy']}' ";
		$this->assign("yewuy",$_REQUEST['yewuy']);


    }

	if(!empty($_REQUEST['nickname'])){

		$wheres.=" and a.nickname LIKE '%".$_REQUEST['nickname']."%'";
		$this->assign("nickname",$_REQUEST['nickname']);
		
	}

    if($user['power'] == 1){
        $wheres .= " and t.partner_id = {$user['uid']}";
    } else if ($user['power'] == 2){
        $wheres .= " and t.team_name = {$user['uid']}";

    }
	$wheres .= " and b.idcard <> ''";
    if(!empty($_REQUEST['level'])&&$_REQUEST['level']!=0){

        $wheres2=" and b.level=".intval($_REQUEST['level']);
        	$this->assign("level",$_REQUEST['level']);
		

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.mobile as phone,a.avatar,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard,a.power FROM ims_mc_members as a inner join  ims_xuan_zy_person as b on b.uid=a.uid  LEFT JOIN ims_xuan_zy_commission_team as t on t.uid = a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM ims_mc_members as a inner join ims_xuan_zy_person as b on b.uid=a.uid  LEFT JOIN ims_xuan_zy_commission_team as t on t.uid = a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";

    }else{

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.avatar,a.mobile as phone,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard,a.power FROM ims_mc_members as a left join ims_xuan_zy_person as b on b.uid=a.uid   LEFT JOIN ims_xuan_zy_commission_team as t on t.uid = a.uid  WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM  ims_mc_members as  a left join ims_xuan_zy_person as b on b.uid=a.uid   LEFT JOIN ims_xuan_zy_commission_team as t on t.uid = a.uid WHERE 1 {$wheres} order by a.uid desc";

    }
    $limit = ($pindex-1)*$psize;
    $sql .= "  limit {$limit},{$psize}";
    $count = DB::query($sql2)[0]['count'];
    if(request()->isajax()){
    $list = DB::query($sql);
    foreach ($list as $k => &$v) {
    	switch ($v['power']) {
    		case '1':
    			$v['power'] = "股东"; 
    			break;
			case '2':
    			$v['power'] = "团队"; 
    			break;
    		
    		default:
    			$v['power'] = "普通用户"; 
    			# code...
    			break;
    	}
    }
	echo json_encode($list);die;
    }
    $this->assign("list",$list);
    $this->assign("count",$count);
	return $this->fetch("admin/team_member");

    }
    public function dl(){
        $user = Session::get("user");
        $sql = "select id from ims_xuan_zy_commission_model where uid = {$user['uid']} and `default` = 1 and type = 0";
        $model_id  = DB::query($sql)[0]['id'];
		$id  = $_REQUEST['id'];
		$self =  $this->random(8);
		$user = DB::table("ims_xuan_zy_person")->where("uid",$id)->find();
		$data = [
			"username"=>$user['name'],
			"password"=>md5("123456".$self),
			"salt"=>$self,
			"power"=>"2",
            "joindate"=>time(),
			"user_id"=>$id
		];

		DB::table("ims_users")->insert($data);
        DB::table("ims_mc_members")->where("uid","{$id}")->setField(["power"=>2]);
		DB::table("ims_xuan_zy_commission_team")->where("uid","{$id}")->setField(["com_model_id"=>$model_id]);
		DB::table("ims_xuan_zy_commission")->where("uid","{$id}")->setField(["power"=>2]);
		echo json_encode(true);
    }
    public function add(){
		
		$id  =	$_REQUEST['id'];
		$sql = "select * from ims_xuan_zy_commission where uid = {$id}";
		$arr = DB::query($sql);
		$this->assign("list",$arr);
		$this->assign("uid",$id);
		return $this->fetch("admin/team_bank");
    }
    public function add_bank(){
    	$id  =	$_REQUEST['id'];
		$uid  =	$_REQUEST['uid'];
		if($id){
			$sql = "select * from ims_xuan_zy_commission where id = {$id}";
			$info = DB::query($sql)[0];
			$this->assign("info",$info);
		}else{
            $sql = "select partner_id from ims_xuan_zy_commission_team where uid = {$uid}";
            $partner_id = DB::query($sql)[0]['partner_id'];
			$sql = "select bank_name from ims_xuan_zy_commission where uid = {$partner_id}";
			$bank = DB::query($sql);
			$this->assign("bank",$bank);
		}
		$this->assign("uid",$uid);
			return $this->fetch("admin/team_bankadd");
    }
    public function upd_bank(){
		$id =$_REQUEST['id'];
		$uid =$_REQUEST['uid'];
        $bank_name = $_REQUEST['bank_name'];
        $commision = isset($_REQUEST['commision'])?$_REQUEST['commision']:0;
        $sql = "select partner_id from ims_xuan_zy_commission_team where uid = {$uid}";
        $partner_id  = DB::query($sql)[0]['partner_id'];

            if(empty($bank_name)){
            $sql = "select bank_name from ims_xuan_zy_commission where id = {$id}";
            $bank_name =DB::query($sql)[0]['bank_name'];

            }
            $sql = "select commision from ims_xuan_zy_commission where uid = {$partner_id} and bank_name = '{$bank_name}'";
            $lower_commision = DB::query($sql)[0]["commision"];

            if($commision > $lower_commision){
                $this->error("设置佣金过多");
            } 

        if($id){

                $sql = "update ims_xuan_zy_commission set commision = '{$commision}' where id = {$id}";
                DB::query($sql);
                $this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/team/add?id={$uid}");
            
         } else {
            $sql  = "select id from ims_xuan_zy_commission where uid = {$uid} and bank_name = '{$_REQUEST['bank_name']}'";
            $bank = DB::query($sql);
            $data['commision'] =  $commision;
            if($bank){
                DB::table("ims_xuan_zy_commission")->where("id",$id)->setField($data);
                $this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/team/add?id={$uid}");
            }else {
                $data['bank_name'] =  $_REQUEST['bank_name'];
                $data['power']=1;
                $data['type']=3;
                $data['uid']=$uid;
                DB::table("ims_xuan_zy_commission")->insert($data); 
                $this->success("添加成功","{$_SERVER['SCRIPT_NAME']}/admin/team/add?id={$uid}");
                
            }

         }
    }
}